package com.cyys.modules.mobile;

import com.alibaba.fastjson.JSONObject;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.AjaxJson;
import com.cyys.common.thirdInterface.CommonUtil;
import com.cyys.common.thirdInterface.wx.config.WxConfig;
import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.wx.model.user.WxUserinfo;
import com.cyys.modules.wx.service.user.WxUserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.cyys.common.thirdInterface.wx.WxNoun.getToken;

@RestController
@RequestMapping("/api/general")
@Api(tags = "通用接口")
public class GeneralAction extends BaseAction {

	@Resource
	private WxUserinfoService wxUserinfoService;


	@PostMapping("/queryParam")
	@ApiOperation(value = "字典查询")
	public AR queryParam(String basicId) throws Exception {
		if (StringUtil.isBlank(basicId)) {
			return AR.failed("大类Id不能为空");
		}
		return AR.ok(ParamBasicUtil.getParamBasicTypeMap().get(basicId),"获取成功");
	}



	/**
	 * @param jsonData 小程序服务消息模板数据(消息模板)
	 * @date 2021/07/28
	 * @author djc
	 * @return
	 */
	@ApiOperation(value = "小程序发送订阅消息通知（服务通知）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "toUserId", value = "接收消息用户ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "jsonData", value = "服务消息模板数据", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "token", value = "token参数", required = true, dataType = "String", paramType = "header")
	})
	@PostMapping(value = "/wx/sendMessage")
	public AjaxJson sendWxTempleMsg(String toUserId, String jsonData) throws Exception {
		String openId = "";
		if(StringUtil.isBlank(toUserId)){
			return error("消息推送失败，缺少接受者信息");
		}
		if(StringUtil.isBlank(jsonData)){
			return error("消息推送失败，缺少消息信息");
		}
		//获取openid
		WxUserinfo byId = wxUserinfoService.getById(toUserId);
		if(byId.getOpenId()==null || StringUtil.isBlank(byId.getOpenId())){
//				return error("消息推送失败，缺少消息模板信息Openid");
			return error("消息推送失败，请提醒对方登录小程序授权消息通知");
		}else{
			openId = byId.getOpenId();
		}
		JSONObject strToJson = JSONObject.parseObject(jsonData);
		strToJson.put("touser",openId);//消息模板--接收人openid
		String templateJson = strToJson.toString();
		//获取小程序accessToken
		String ACCESS_TOKEN = getToken();

		String wxSendmessageUrl1 = WxConfig.WX_SENDMESSAGE_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);
		// 发起请求，返回JSON字符串
		JSONObject jsonObject = CommonUtil.httpsRequest(wxSendmessageUrl1, "POST", templateJson);

		if("0".equals(jsonObject.get("errcode"))||"ok".equals(jsonObject.get("errmsg"))){
			return success("消息推送成功");
		}else{
			return error("消息推送失败");
		}

	}

}
